package com.yvan.kit.image;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {

    /**
     * 图像类型转换
     * GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
     */
    public static void convert(String source, String result) {
        try {
            File f = new File(source);
            f.canRead();
            f.canWrite();
            BufferedImage src = ImageIO.read(f);
            ImageIO.write(src, "JPG", new File(result));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static int[] getSize(File file) {
        int[] size = new int[2];
        BufferedImage bi = null;
        int srcWidth = 0;
        try {
            bi = ImageIO.read(file);
            if (bi != null) {
                size[0] = bi.getWidth();
                size[1] = bi.getHeight();
            }
            srcWidth = bi.getHeight(); // 源图宽度
        } catch (IOException e) {
            e.printStackTrace();
        }
        return size;
    }

}
